package com.hrbu.mp.generator.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.hrbu.mp.generator.handler.MyMetaObjectHandler;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;

/**
 * @Description:
 * @Author: tanyajun
 * @CreateTime: 2021/4/1
 * @Blog: https://tanyajun.top
 */
@Configuration
@ComponentScan(value = "com.hrbu.mp.generator",
        excludeFilters = { @ComponentScan.Filter(type= FilterType.ANNOTATION,classes={Controller.class}),
        })
@PropertySource("classpath:jdbc.properties")
@MapperScan("com.hrbu.mp.generator.mapper")
public class SpringConfig {
        @Value("${jdbc.url}")
        private String url;
        @Value("${jdbc.driver}")
        private String driver;
        @Value("${jdbc.username}")
        private String username;
        @Value("${jdbc.password}")
        private String password;

        /**
         * 配置数据源
         * @return
         */
        @Bean("dataSource")
        public DruidDataSource dataSource(){
                DruidDataSource druidDataSource = new DruidDataSource();
                druidDataSource.setDriverClassName(driver);
                druidDataSource.setUrl(url);
                druidDataSource.setUsername(username);
                druidDataSource.setPassword(password);
                return druidDataSource;
        }

        /**
         * 配置sqlSessionFactory
         * @return
         */
        @Bean("sqlSessionFactory")
        public MybatisSqlSessionFactoryBean sqlSessionFactory(){
                MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
                // 设置数据源
                sessionFactoryBean.setDataSource(dataSource());
                // 设置配置
                sessionFactoryBean.setConfiguration(configuration());
                // 设置枚举类
                sessionFactoryBean.setTypeEnumsPackage("com.hrbu.mp.generator.myenum");
                // 设置内置分页插件
                sessionFactoryBean.setPlugins(new OptimisticLockerInterceptor(),new PaginationInterceptor());
                return sessionFactoryBean;
        }

        /**
         * 设置基础配置
         * @return
         */
        @Bean("configuration")
        public MybatisConfiguration configuration(){
                MybatisConfiguration configuration = new MybatisConfiguration();
                // 日志
                configuration.setLogImpl(StdOutImpl.class);
                //  全局配置
                configuration.setGlobalConfig(globalConfig());
                return configuration;
        }

        /**
         * 设置全局配置
         * @return
         */
        @Bean("globalConfig")
        public GlobalConfig globalConfig(){
                GlobalConfig globalConfig = new GlobalConfig();
                // 设置逻辑删除
                globalConfig.setDbConfig(dbConfig());
                // 设置自动填充
                globalConfig.setMetaObjectHandler(new MyMetaObjectHandler());
                return globalConfig;
        }

        /**
         * 配置逻辑删除
         * @return
         */
        @Bean("dbConfig")
        public GlobalConfig.DbConfig dbConfig(){
                GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
                // 已删除==1
                dbConfig.setLogicDeleteValue("1");
                // 未删除==0
                dbConfig.setLogicNotDeleteValue("0");
                return dbConfig;
        }

        /**
         * 配置事务处理器
         * @return
         */
        @Bean("transactinManager")
        public DataSourceTransactionManager dataSourceTransactionManager(){
                DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
                dataSourceTransactionManager.setDataSource(dataSource());
                return dataSourceTransactionManager;
        }
}
